import { defineStore } from 'pinia'
import { InstanceApi, InstanceVO } from '@/api/awards/instance'
import {
  ReviewSceneApi,
  SCXInstanceInitialRemainingVO,
  StageRoundReqQuotaVO,
  StageRoundVO
} from '@/api/awards/reviewscene'

export const useInstanceReviewSceneStore = defineStore('instanceReviewScene', () => {
  const awardsId = ref('') // 评奖ID
  const awardsInfo = ref<InstanceVO>({} as InstanceVO) // 评奖信息
  const currentRoundRemainList = ref<StageRoundReqQuotaVO[]>([]) // 评奖轮次剩余名额列表
  const scxInitialRemaining = ref<SCXInstanceInitialRemainingVO>(
    {} as SCXInstanceInitialRemainingVO
  ) // SCX初评剩余名额
  const isFinalResultConfirmed = ref(false) // 终评结果是否已确认

  const currentStage = ref<StageRoundVO>() // 当前阶段
  // 当前是不是终评结束了
  const isFinalEnd = computed(() => {
    return currentStage.value?.voteState === 'END'
  })
  // 当前轮次
  const currentRound = computed(() => {
    return currentStage.value?.round.find((item) => item.currentRound)
  })
  // 当前轮次评审方式
  const currentRoundReviewMethod = computed(() => currentRound.value?.reviewMethod)

  // 设置评奖ID
  const setAwardsId = (id: string) => {
    awardsId.value = id
  }

  // 设置评奖信息
  const setAwardsInfo = (info: InstanceVO) => {
    awardsInfo.value = info
  }

  // 查询当前阶段当前轮次信息
  const getCurrentStageRound = async () => {
    try {
      const list = await ReviewSceneApi.getStageRound(awardsId.value)
      if (!list || list.length === 0) {
        return
      }
      if (list.length === 1) {
        currentStage.value = list[0]
        return
      }
      if (list.length === 2) {
        const [initialStage, finalStage] = list
        if (finalStage.voteState === 'NOT_STARTED') {
          currentStage.value = initialStage
          return
        }
        currentStage.value = finalStage
      }
    } catch {}
  }

  // 查询评奖基本信息
  const getAwardsInfo = async () => {
    if (!currentStage.value) {
      return
    }
    try {
      awardsInfo.value = await InstanceApi.getTeacherInstance(
        awardsId.value,
        currentStage.value.name
      )
    } catch {}
  }

  // 查询当前阶段轮次剩余名额
  const getStageRoundRemaining = async () => {
    // 没有评奖信息或当前轮次或者终评已结束
    if (!awardsInfo.value || !currentRound.value || currentStage.value?.voteState === 'END') {
      return
    }
    // console.log('查询阶段轮次剩余名额', awardsId.value, currentRound.value?.roundId)
    try {
      currentRoundRemainList.value = await ReviewSceneApi.getJudgesStageRoundQuota(
        awardsId.value,
        ''
      )
      // console.log('当前阶段轮次剩余名额', currentRoundRemainList.value)
    } catch {}
  }

  // 查询SCX初评剩余名额
  const getSCXInitialRemaining = async () => {
    if (!currentStage.value || currentStage.value.name !== '初评'|| currentStage.value.voteState === 'END') {
      return
    }
    try {
      scxInitialRemaining.value = await ReviewSceneApi.getScxInitialRemainingCount(awardsId.value)
    } catch {}
  }

  const updateInitialRemaining = () => {
    getStageRoundRemaining().then()
    if (awardsInfo.value?.awardsType === 1 && currentStage.value?.name === '初评') {
      getSCXInitialRemaining().then()
    }
  }
  
  // 查询是否终评结果已确认
  const getFinalResultConfirmed = async () => {
    if (!awardsId.value || !currentRound.value) {
      return
    }
    try {
      isFinalResultConfirmed.value = await ReviewSceneApi.isJudgesConfirmResult(awardsId.value, currentRound.value.roundId)
    } catch {}
  }

  // 重置
  const reset = () => {
    awardsId.value = ''
    awardsInfo.value = {} as InstanceVO
    currentStage.value = undefined
    scxInitialRemaining.value = {} as SCXInstanceInitialRemainingVO
    currentRoundRemainList.value = []
    isFinalResultConfirmed.value = false
  }

  return {
    awardsId,
    awardsInfo,
    currentRoundRemainList,
    scxInitialRemaining,
    currentStage,
    isFinalEnd,
    currentRound,
    currentRoundReviewMethod,
    isFinalResultConfirmed,
    setAwardsId,
    setAwardsInfo,
    getCurrentStageRound,
    getAwardsInfo,
    getSCXInitialRemaining,
    getStageRoundRemaining,
    updateInitialRemaining,
    getFinalResultConfirmed,
    reset
  }
})
